import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:photo_view/photo_view_gallery.dart';
import 'package:provider/provider.dart';
import 'package:sliding_up_panel/sliding_up_panel.dart';
import 'package:wallpaper/db/love_database.dart';
import 'package:wallpaper/tools/down_image.dart';

// ignore: must_be_immutable
class ImageView extends StatefulWidget {
  final List images;
  int selectedIndex;
  final VoidCallback resetGetdata;
  bool isMobile = false;
  ImageView({
    super.key,
    required this.images,
    required this.selectedIndex,
    required this.resetGetdata,
    this.isMobile = false,
  });

  @override
  State<ImageView> createState() => _ImageViewState();
}

class _ImageViewState extends State<ImageView> {
  final PanelController _panelController = PanelController();
  bool isLove = false;

  void isLoveGet() async {
    bool res = await context.read<LoveDatabase>().isLove(widget.isMobile
        ? widget.images[widget.selectedIndex].largePath
        : widget.images[widget.selectedIndex].smallPath);
    setState(() {
      isLove = res;
    });
  }

  @override
  void initState() {
    super.initState();
    isLoveGet();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        color: Colors.black,
        // 导航栏字体颜色
        child: AnnotatedRegion<SystemUiOverlayStyle>(
          value: SystemUiOverlayStyle.light,
          child: Stack(
            children: [
              GestureDetector(
                onTap: () {
                  _panelController.isPanelOpen
                      ? _panelController.close()
                      : _panelController.open();
                },
                child: PhotoViewGallery.builder(
                  itemCount: widget.images.length,
                  pageController:
                      PageController(initialPage: widget.selectedIndex),
                  builder: (context, index) {
                    return PhotoViewGalleryPageOptions(
                      imageProvider:
                          NetworkImage(widget.images[index].largePath),
                    );
                  },
                  onPageChanged: (index) {
                    setState(() {
                      widget.selectedIndex = index;
                      isLoveGet();
                    });
                  },
                ),
              ),
              // 滑动面板
              SlidingUpPanel(
                color: const Color.fromARGB(73, 158, 158, 158),
                controller: _panelController,
                minHeight: 0, // 面板最小高度
                maxHeight:
                    MediaQuery.of(context).size.height * 0.3, // 最大高度为屏幕 30%
                borderRadius: BorderRadius.vertical(top: Radius.circular(10.0)),
                parallaxEnabled: true, // 启用视差效果
                parallaxOffset: 0.2, // 视差偏移量
                backdropEnabled: true, // 背景遮罩
                backdropOpacity: 0.5, // 背景遮罩不透明度
                panel: _buildPanelContent(), // 面板内容
                collapsed: null, // 收起时的显示内容
                onPanelOpened: () => {},
                onPanelClosed: () => {},
              ),
            ],
          ),
        ));
  }

  // 面板展开时的内容
  Widget _buildPanelContent() {
    double iconSize = 30;
    List btnList = [
      IconButton(
        onPressed: () async {
          // 下载图片到相册
          await DownImage.saveImage(
              context, widget.images[widget.selectedIndex].largePath);
        },
        icon: Icon(Icons.downloading, size: iconSize, color: Colors.white),
      ),
      IconButton(
        onPressed: () {
          setState(() {
            isLove = !isLove;
          });
          context.read<LoveDatabase>().addLove(
              widget.isMobile
                  ? widget.images[widget.selectedIndex].largePath
                  : widget.images[widget.selectedIndex].smallPath,
              widget.images[widget.selectedIndex].largePath,
              widget.images[widget.selectedIndex].tags);
          widget.resetGetdata();
        },
        icon: isLove
            ? Icon(Icons.favorite, size: iconSize, color: Colors.red)
            : Icon(Icons.favorite_border, size: iconSize, color: Colors.white),
      ),
      IconButton(
        onPressed: () {
          // 分享图片 复制图片地址到剪贴板
          Clipboard.setData(ClipboardData(
              text: widget.images[widget.selectedIndex].largePath));
       
        },
        icon: Icon(Icons.share, size: iconSize, color: Colors.white),
      )
    ];
    return Column(
      children: [
        // 拖拽提示条
        GestureDetector(
          onTap: () => _panelController.close(),
          child: Container(
            margin: EdgeInsets.symmetric(vertical: 10),
            height: 5,
            width: 50,
            decoration: BoxDecoration(
              color: Colors.grey[300],
              borderRadius: BorderRadius.circular(12),
            ),
          ),
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [],
        ),
        Expanded(
            child: Padding(
          padding: const EdgeInsets.only(left: 10, right: 10),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              for (var btn in btnList) btn,
            ],
          ),
        ))
      ],
    );
  }
}
